import { _decorator, Component, instantiate, math, Node, Prefab } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnemyManager')
export class EnemyManager extends Component {

    @property
    enemy0SpawnRate: number = 3;
    @property(Prefab)
    enemy0Prefab: Prefab = null

    @property
    enemy1SpawnRate: number = 5;
    @property(Prefab)
    enemy1Prefab: Prefab = null

    @property
    enemy2SpawnRate: number = 15;
    @property(Prefab)
    enemy2Prefab: Prefab = null

    @property
    rewardSpawnRate: number = 20;
    @property(Prefab)
    reward1Prefab: Prefab = null
    @property(Prefab)
    reward2Prefab: Prefab = null


    start() {
        //str1 调用方法  str2: 调用频率  使用定时器的方式进行生成
        this.schedule(this.enemy0Spawn, this.enemy0SpawnRate);
        this.schedule(this.enemy1Spawn, this.enemy1SpawnRate);
        this.schedule(this.enemy2Spawn, this.enemy1SpawnRate);
        this.schedule(this.rewarwdSpawn, this.enemy1SpawnRate);
    }

    update(deltaTime: number) {

    }

    protected onDestroy(): void {
        this.unschedule(this.enemy0Spawn);
        this.unschedule(this.enemy1Spawn);
        this.unschedule(this.enemy2Spawn);
        this.unschedule(this.rewarwdSpawn);
    }

    rewarwdSpawn() {
        const randomNum = math.randomRangeInt(0, 2);
        let Prefab = null;
        if (randomNum == 0) {
            Prefab = this.reward1Prefab;
        } else {
            Prefab = this.reward2Prefab;
        }
        this.objectSpanwn(Prefab, -207, 207, 474);
    }

    enemy0Spawn() {
        this.objectSpanwn(this.enemy0Prefab, -200, 200, 450);
    }
    enemy1Spawn() {
        this.objectSpanwn(this.enemy1Prefab, -200, 200, 475);
    }
    enemy2Spawn() {
        this.objectSpanwn(this.enemy2Prefab, -155, 155, 560);
    }

    objectSpanwn(enemyPrefab: Prefab, minX: number, maxX: number, Y: number) {
        const enemy = instantiate(enemyPrefab);
        this.node.addChild(enemy);
        const randomX = math.randomRangeInt(minX, maxX);
        enemy.setPosition(randomX, Y, 0);
    }

}


